package zhoug.common.view.ratio;

import android.content.Context;
import android.util.AttributeSet;

/**
 * 支持宽高比例的ImageView
 * @Author 35574
 * @Date 2020/7/30
 * @Description
 */
public class ZRatioImageView extends androidx.appcompat.widget.AppCompatImageView {
    private static final String TAG = ">>>ZRatioImageView";
    private static final boolean DEBUG = false;
    private ZRatioViewHelper _ratioViewHelper;

    public ZRatioImageView(Context context) {
        this(context, null);
    }

    public ZRatioImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZRatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        _getRatioViewHelper().parseAttr(context,attrs);
    }

    private ZRatioViewHelper _getRatioViewHelper(){
        if(_ratioViewHelper==null){
            _ratioViewHelper=new ZRatioViewHelper();
        }
        return _ratioViewHelper;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        int[] measureSpecs = _getRatioViewHelper().measureSpec(this, widthMeasureSpec, heightMeasureSpec);
        if(measureSpecs!=null){
            super.onMeasure(measureSpecs[0], measureSpecs[1]);;
        }
    }

    public void setRatio(float ratio) {
        _getRatioViewHelper().setRatio(ratio);
    }

}
